package farmers.javcap.bean;

import farmers.javcap.bean.ThreadBean;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

public class ThreadUtil {

    public static ThreadBean 获取url单个(String url, int index) {
        ThreadBean thread = new ThreadBean();

        try {
            String httpUrl = url + String.format("%03d", index);
//            System.out.println(httpUrl);

            Document document = Jsoup.connect(httpUrl).get();
//            System.out.println(document);

            // 图片文件名
            Elements elements_title = document.getElementsByClass("title title-multi-ellipsis card-title");
//            System.out.println(elements_title);
            elements_title.forEach(title -> {
                String html = title.html();
//                System.out.println(html);

                thread.setFileName(html);
                thread.setImgName(html + ".jpg");
            });

            // 图片url下载地址
            Elements class_img = document.getElementsByTag("video");
//            System.out.println(class_img);
            class_img.forEach(img -> {
                String poster = img.attr("poster");
//                System.out.println(poster);
                thread.setImgUrl(poster);
            });


            // 视频
            Elements class_video = document.getElementsByTag("meta");
//            System.out.println(class_video);
            class_video.forEach(img -> {
                String itemprop = img.attr("itemprop");
//                System.out.println(itemprop);

                if("contentUrl".equals(itemprop)){
                    String content = img.attr("content");
//                    System.out.println(content);
                    thread.setVideoName(thread.getFileName() + ".mp4");
                    thread.setVideoUrl(content);
                }

            });

            if(StringUtils.isEmpty(thread.getImgName()) || StringUtils.isEmpty(thread.getImgUrl())){
                System.out.print("正在解析\t" + httpUrl + "\t" + thread.getImgName() + "\t" + thread.getImgUrl() + "\t" + thread.getVideoUrl());
            }else{
                System.out.println("正在解析\t" + httpUrl + "\t" + thread.getImgName() + "\t" + thread.getImgUrl() + "\t" + thread.getVideoUrl());
            }

            // 去除特殊制服
            thread.setImgName(thread.getImgName().replaceAll("@", ""));
            thread.setImgName(thread.getImgName().replaceAll("[#＃]", ""));
            thread.setImgName(thread.getImgName().replaceAll("●", ""));

//            thread.setFileName(thread.getFileName().replaceAll("@", ""));
//            thread.setFileName(thread.getFileName().replaceAll("[#＃]", ""));
//
//            thread.setVideoName(thread.getVideoName().replaceAll("@", ""));
//            thread.setVideoName(thread.getVideoName().replaceAll("[#＃]", ""));
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        return thread;
    }
}